local PANEL = {}

local StatsData = {}
	StatsData["money"] = 0
	StatsData["kills"] = 0
	StatsData["deaths"] = 0
	StatsData["unique_id"] = 0
	
	StatsData["Mining"] = 0
	StatsData["Smithing"] = 0
	StatsData["Farming"] = 0
	StatsData["Cooking"] = 0
	StatsData["Woodcutting"] = 0
	StatsData["Carpentry"] = 0
	StatsData["Survival"] = 0
	StatsData["Technology"] = 0
	StatsData["Strength"] = 0
	StatsData["Health"] = 0
	
	StatsData["MiningEXP"] = 0
	StatsData["SmithingEXP"] = 0
	StatsData["FarmingEXP"] = 0
	StatsData["CookingEXP"] = 0
	StatsData["WoodcuttingEXP"] = 0
	StatsData["CarpentryEXP"] = 0
	StatsData["SurvivalEXP"] = 0
	StatsData["TechnologyEXP"] = 0
	StatsData["StrengthEXP"] = 0
	StatsData["HealthEXP"] = 0

local SpawnMenu = nil
vgui_Spawn = vgui.RegisterTable( PANEL, "DPanel" )

function PANEL:Init()
	self:SetText( "" )
	self.Buttons = {}
	self.BottomButtons = {}
	self.IconList = {}
	self:SetSkin( GAMEMODE.HudSkin )
	
	self.pnlMain = vgui.Create( "DPanel", self )
		
	self.btnCancel = vgui.Create( "DButton", self )
		self.btnCancel:SetText( "#Close" )
		self.btnCancel:SetSize( 100, 30 )
		self.btnCancel:SetFGColor( Color( 0, 0, 0, 200 ) )
		self.btnCancel:SetFont( "FRETTA_SMALL" )
		self.btnCancel.DoClick = function() self:CloseMenu() end
		self.btnCancel:SetVisible( false )
		
		Derma_Hook( self.btnCancel, "Paint", 				"Paint", 		"CancelButton" )
		Derma_Hook( self.btnCancel, "PaintOver", 			"PaintOver", 	"CancelButton" )
		Derma_Hook( self.btnCancel, "ApplySchemeSettings", 	"Scheme", 		"CancelButton" )
		Derma_Hook( self.btnCancel, "PerformLayout", 		"Layout", 		"CancelButton" )
		

		
	self:PerformLayout()
	
	self.OpenTime = SysTime()
	
end

function PANEL:NoFadeIn()
	self.OpenTime = 0
end

function PANEL:AddBottomButton( text, pnlfnc, txt )
	local btn = vgui.Create( "DButton", self.pnlMain )
	btn:SetText( text )
	btn:SetSize( 200, 30 )
	btn:SetVisible( true )

	btn.DoClick = function() pnlfnc() surface.PlaySound( Sound("buttons/lightswitch2.wav") ) SpawnMenu:CloseMenu() end
	
	Derma_Hook( btn, "Paint", 				"Paint", 		"PanelButton" )
	Derma_Hook( btn, "PaintOver",			"PaintOver", 	"PanelButton" )
	Derma_Hook( btn, "ApplySchemeSettings", "Scheme", 		"PanelButton" )
	Derma_Hook( btn, "PerformLayout", 		"Layout", 		"PanelButton" )
	
	table.insert( self.BottomButtons, btn )

	if ( txt ) then
		btn.OnCursorEntered = function() self.OldHoverText = self:GetHoverText() self:SetHoverText( txt ) end
		btn.OnCursorExited = function() self:SetHoverText( self.OldHoverText ) self.OldHoverText = nil end
	end
	
	return btn

end

function PANEL:ClearSelectedPanel()

	self.pnlMain:Clear()

	for k, btn in pairs( self.BottomButtons ) do
	
		btn.m_bSelected = false
		if ( IsValid( btn.pPanel ) ) then
			btn.pPanel:SetVisible( false )
		end
	
	end

end

function PANEL:AddSelectButton( strName, fnFunction, txt )

	local btn = vgui.Create( "DButton", self.pnlButtons )
	btn:SetText( strName )
	btn:SetSize( 200, 30 )
	btn.DoClick = function() fnFunction() surface.PlaySound( Sound("buttons/lightswitch2.wav") ) self:CloseMenu() end
	
	Derma_Hook( btn, "Paint", 				"Paint", 		"SelectButton" )
	Derma_Hook( btn, "PaintOver",			"PaintOver", 	"SelectButton" )
	Derma_Hook( btn, "ApplySchemeSettings", "Scheme", 		"SelectButton" )
	Derma_Hook( btn, "PerformLayout", 		"Layout", 		"SelectButton" )
	
	self.pnlButtons:AddItem( btn )
	
	table.insert( self.SelectButtons, btn )
	return btn
	
end

function PANEL:AddCancelButton()

	self.btnCancel:SetVisible( true )
	
end

function PANEL:PerformLayout()

	self:SetSize( ScrW(), ScrH() )
	
	local CenterY = ScrH() * 0.5
	local CenterX = ScrW() * 0.5
	
	self.btnCancel:SetPos( ScrW() - self.btnCancel:GetWide() - 20, ScrH() - self.btnCancel:GetTall() - 20 )
	
	self.pnlMain:SetPos( CenterX, 0 )
	self.pnlMain:SetSize( CenterX, ScrH() )
	
	local x = CenterX + 20
	for k, btn in pairs( self.BottomButtons ) do
--		btn:SetPos( x, ScrH() - 50 )
		x = x + btn:GetWide() + 20		
	end
end

function PANEL:GetMainPanelPos()
	return self.pnlMain:GetPos()
end

function PANEL:Paint()
		
	local CenterY = ScrH() * 0.5
	local CenterX = ScrW() * 0.5
	
	surface.SetDrawColor( 0, 0, 0, 200 );
	surface.DrawRect( CenterX, 0, CenterX, ScrH() );

	GAMEMODE:PaintSplashScreen( self:GetWide(), self:GetTall() )

end

local function menuDropWeapon()
	RunConsoleCommand("DropWeapon")
end

local function saveStatsButton()
	RunConsoleCommand("ks_SaveStats")
end

local function updateStats()
	local ply = LocalPlayer()
	StatsData["money"] = ply:GetNWInt("money")
	StatsData["kills"] = ply:GetNWInt("kills")
	StatsData["deaths"] = ply:GetNWInt("deaths")
	StatsData["unique_id"] = ply:GetNWInt ("SteamID")
	
	StatsData["Mining"] = ply:GetNWInt("Mining")
	StatsData["Smithing"] = ply:GetNWInt("Smithing")
	StatsData["Farming"] = ply:GetNWInt("Farming")
	StatsData["Cooking"] = ply:GetNWInt("Cooking")
	StatsData["Woodcutting"] = ply:GetNWInt("Woodcutting")
	StatsData["Carpentry"] = ply:GetNWInt("Carpentry")
	StatsData["Survival"] = ply:GetNWInt("Survival")
	StatsData["Technology"] = ply:GetNWInt("Technology")
	StatsData["Strength"] = ply:GetNWInt("Strength")
	StatsData["Health"] = ply:GetNWInt("Health")
	
	StatsData["MiningEXP"] = ply:GetNWInt("MiningExp")
	StatsData["SmithingEXP"] = ply:GetNWInt("SmithingExp")
	StatsData["FarmingEXP"] = ply:GetNWInt("FarmingExp")
	StatsData["CookingEXP"] = ply:GetNWInt("CookingExp")
	StatsData["WoodcuttingEXP"] = ply:GetNWInt("WoodcuttingExp")
	StatsData["CarpentryEXP"] = ply:GetNWInt("CarpentryExp")
	StatsData["SurvivalEXP"] = ply:GetNWInt("SurvivalExp")
	StatsData["TechnologyEXP"] = ply:GetNWInt("TechnologyExp")
	StatsData["StrengthEXP"] = ply:GetNWInt("StrengthExp")
	StatsData["HealthEXP"] = ply:GetNWInt("HealthExp")
end

local function addStatsToList( Stats )
	Stats:SetPos(5, 20)
	Stats:SetSize(ScrW() * 0.5 - 20, 190)
	Stats:SetMultiSelect(false)
	Stats:AddColumn("Stats")
	Stats:AddColumn("Level")
	Stats:AddColumn("Exp")

	updateStats()
	
	Stats:AddLine("Survival", StatsData["Survival"], StatsData["SurvivalEXP"])
	Stats:AddLine("Smithing", StatsData["Smithing"], StatsData["SmithingEXP"])
	Stats:AddLine("Farming", StatsData["Farming"], StatsData["FarmingEXP"])
	Stats:AddLine("Cooking", StatsData["Cooking"], StatsData["CookingEXP"])
	Stats:AddLine("Woodcutting", StatsData["Woodcutting"], StatsData["WoodcuttingEXP"])
	Stats:AddLine("Carpentry", StatsData["Carpentry"], StatsData["CarpentryEXP"])
	Stats:AddLine("Mining", StatsData["Mining"], StatsData["MiningEXP"])
	Stats:AddLine("Technology", StatsData["Technology"], StatsData["TechnologyEXP"])
	Stats:AddLine("Strength", StatsData["Strength"], StatsData["StrengthEXP"])
	Stats:AddLine("Health", StatsData["Health"], StatsData["HealthEXP"])
end

local function addPropsToList( _ListProps )
	_ListProps:SetPos( 5, 5 )
	_ListProps:SetSize( ScrW() * 0.5-20, ScrH() - 120)
	local CollapsibleCategoryTable = {}
	local iteration = 1
	for spawnType,typesTable in pairs( ks_SpawnLists ) do
			-- add new category header for each type of spawnlist
			CollapsibleCategoryTable[spawnType] = {}
			CollapsibleCategoryTable[spawnType] = vgui.Create("DCollapsibleCategory", _ListProps)
			CollapsibleCategoryTable[spawnType]:SetPos( 25,50*iteration )
			CollapsibleCategoryTable[spawnType]:SetSize( ScrW() / 2 - 76, 50 )
			CollapsibleCategoryTable[spawnType]:SetExpanded( 0 )
			CollapsibleCategoryTable[spawnType]:SetLabel( spawnType )
			_ListProps:AddItem(CollapsibleCategoryTable[spawnType])
			iteration = iteration + 1
			
			SpawnMenu.IconList[spawnType] = vgui.Create( "DPanelList", CollapsibleCategoryTable[spawnType] )
			SpawnMenu.IconList[spawnType]:EnableVerticalScrollbar( true ) 
			SpawnMenu.IconList[spawnType]:EnableHorizontal( true ) 
			SpawnMenu.IconList[spawnType]:SetAutoSize( true )
			SpawnMenu.IconList[spawnType]:SetSpacing( 5 )
			SpawnMenu.IconList[spawnType]:SetPadding( 4 ) 
			SpawnMenu.IconList[spawnType]:SetVisible( true )
			CollapsibleCategoryTable[spawnType]:SetContents( SpawnMenu.IconList[spawnType] )
			_ListProps:AddItem(CollapsibleCategoryTable[spawnType])
			-- make a list of icons for each model in the spawnlist
			for tooltip,modelPath in pairs (typesTable) do
				local ListSpawnIcon = vgui.Create( "SpawnIcon", SpawnMenu.IconList[spawnType] )
				ListSpawnIcon:SetModel(modelPath)
				ListSpawnIcon.DoClick = function( ListSpawnIcon ) RunConsoleCommand("ks_spawn", modelPath, 0) end
				ListSpawnIcon:SetIconSize( 64 ) 
				ListSpawnIcon:InvalidateLayout( true ) 
				ListSpawnIcon:SetToolTip( Format( "%s", tooltip ) ) 
				SpawnMenu.IconList[spawnType]:AddItem( ListSpawnIcon )
			end
	end
end


function GM:ShowSpawnMenu()
	if ( !IsValid( SpawnMenu ) ) then
		SpawnMenu = vgui.CreateFromTable( vgui_Spawn )
		SpawnMenu:NoFadeIn()
		SpawnMenu:AddCancelButton()
		SpawnMenu:SetZPos(10)
		local x,y = SpawnMenu:GetMainPanelPos()
	
		local PropertySheet = vgui.Create( "DPropertySheet", SpawnMenu )
		PropertySheet:SetPos( x, y )
		PropertySheet:SetSize( ScrW() * 0.5, ScrH() - 70 )
		
		--///////////////////////////
		-- page one 		Stats
		--///////////////////////////
		local SheetStats = vgui.Create( "DPanel" )
		SheetStats:SetSize( ScrW() * 0.5 - 25, ScrH() - 90 )
		SheetStats:SetPos( ScrW() * 0.5 - 10 , -10 )
	
		SheetStats.Paint = function ()
			surface.SetDrawColor( 24, 37, 49, 255 ) -- What color ( R, B, G, A )
			surface.DrawRect( 0, 0, ScrW() * 0.5 - 10, ScrH() - 80 ) -- How big is it (cords)
		end
	
		local StatsPage = vgui.Create("DListView", SheetStats)
		addStatsToList(StatsPage)
	
		--///////////////////////////
		-- page two			Props
		--///////////////////////////
		local SheetProps = vgui.Create( "DPanel" )
		SheetProps:SetSize( ScrW() * 0.5 - 90, ScrH() - 80 )
		SheetProps:SetPos( ScrW() * 0.5, 0)
	
		SheetProps.Paint = function ()
			surface.SetDrawColor( 24, 37, 49, 255 ) -- What color ( R, B, G, A )
			surface.DrawRect( 0, 0, ScrW() - 5, ScrH() - 75 ) -- How big is it (cords)
		end

		local ListProps = vgui.Create( "DPanelList", SheetProps )
		addPropsToList(ListProps)
		--///////////////////////////
		-- page three
		--///////////////////////////
		local SheetItemThree = vgui.Create( "DPanel" )
		SheetItemThree:SizeToContents()
	
		SheetItemThree.Paint = function ()
			surface.SetDrawColor( 24, 37, 49, 255 ) -- What color ( R, B, G, A )
			surface.DrawRect( 0, 0, ScrW() - 5, ScrH() - 75 ) -- How big is it (cords)
		end

		local SheetItemFour = vgui.Create( "DPanel" )
		SheetItemFour:SizeToContents()
	
		SheetItemFour.Paint = function ()
			surface.SetDrawColor( 24, 37, 49, 255 ) -- What color ( R, B, G, A )
			surface.DrawRect( 0, 0, ScrW() - 5, ScrH() - 75 ) -- How big is it (cords)
		end	
		
		PropertySheet:AddSheet( "Stats", SheetStats, "gui/silkicons/user", false, false, "Lists your skills/exp/statistics" )
		PropertySheet:AddSheet( "Building", SheetProps, "gui/silkicons/brick_add", false, false, "Props and other useful things" )
		PropertySheet:AddSheet( "Tools", SheetItemThree, "gui/silkicons/wrench_orange", false, false, "Props and other useful things" )
		PropertySheet:AddSheet( "Money", SheetItemFour, "gui/silkicons/money", false, false, "Props and other useful things" )
		--PropertySheet:AddSheet( "Building", SheetItemTwo, "materials/silkicons/building", false, false, "Props and other useful things" )
	
		local Button = SpawnMenu:AddBottomButton( "Drop Weapon", menuDropWeapon )
			Button.m_colBackground = Color(0, 90, 90, 255)
			Button:SetPos(20, ScrH() - 50)
	
		local Button2 = SpawnMenu:AddBottomButton( "Save Stats", saveStatsButton )
			Button2.m_colBackground = Color(0, 90, 90, 255)
			Button2:SetPos(240, ScrH() - 50)
	
		SpawnMenu:PerformLayout()
	
		SpawnMenu:AddCancelButton()
	
		SpawnMenu:MakePopup()
	else
		updateStats()
		SpawnMenu:SetVisible(true)
		gui.EnableScreenClicker(true)
	end
end

function GM:CloseSpawnMenu()
	SpawnMenu:CloseMenu()
end

function PANEL:CloseMenu()
	self:SetVisible(false)
	gui.EnableScreenClicker(false)
end
